<?php
class User extends AppModel {

	var $name = 'User';
	var $validate = array(
                'name' => array('rule'=>array('between',2,50),'allowEmpty'=>false,'message'=>'Between 2 and 50 characters'),
		'username' => array(
                                'between'=>array('rule'=>array('between',3,50),'allowEmpty'=>false,'message'=>'Between 3 and 50 characters'),
                                'is_unique'=>array('rule'=>array('check_unique','username'),'message'=>'Login already taken')
                ),
                'password' => array(
                                'equal_passwords'=>array('rule' => 'equal_passwords','message' => 'Passwords dont equal'),
                                'minlength'=>array('rule'=>array('minLength',5),'message'=>'Min length is 5 characters')
                ),
                'password_2' => array(
                                'equal_passwords'=>array('rule' => 'equal_passwords','message' => 'Passwords dont equal'),
                                'minlength'=>array('rule'=>array('minLength',5),'allowEmpty'=>false,'message'=>'Min length is 5 characters')),
		'email' => array('rule'=>'email','message'=>'Please insert a valid email'),
		'info_email' => 'numeric',
	);

	//The Associations below have been created with all possible keys, those that are not needed can be removed
	var $belongsTo = array(
			'Group' =>
				array('className' => 'Group',
					'foreignKey' => 'group_id',
						'conditions' => '',
						'fields' => '',
						'order' => '',
						'limit' => '',
						'offset' => '',
						'dependent' => '',
						'exclusive' => '',
						'finderQuery' => '',
						'counterQuery' => ''
				)
	);
	
	var $hasMany = array(
			'Article' =>
				array('className' => 'Article',
						'foreignKey' => 'user_id',
						'conditions' => '',
						'fields' => '',
						'order' => '',
						'limit' => '',
						'offset' => '',
						'dependent' => '',
						'exclusive' => '',
						'finderQuery' => '',
						'counterQuery' => ''
				),

			'Comment' =>
				array('className' => 'Comment',
						'foreignKey' => 'user_id',
						'conditions' => '',
						'fields' => '',
						'order' => '',
						'limit' => '',
						'offset' => '',
						'dependent' => '',
						'exclusive' => '',
						'finderQuery' => '',
						'counterQuery' => ''
				),

			'News' =>
				array('className' => 'News',
						'foreignKey' => 'user_id',
						'conditions' => '',
						'fields' => '',
						'order' => '',
						'limit' => '',
						'offset' => '',
						'dependent' => '',
						'exclusive' => '',
						'finderQuery' => '',
						'counterQuery' => ''
				),

	);

    function equal_passwords($data) {
        if ($this->data['User']['password_2'] === $this->data['User']['password']) {
            return true;
        } else {
            return false;
        }
    }

    function check_unique($data, $fieldName) {
        $valid = false;
        if(isset($fieldName) && $this->hasField($fieldName))
        {
            $valid = $this->isUnique(array($fieldName => $data));
        }
        return $valid;
   }

    
    function save_user($data) {
        if(!empty($this->data)) {
            $data['User']['password'] = Security::hash(Configure::read('Security.salt') . $data['User']['password']);
            $data['User']['password_2'] = Security::hash(Configure::read('Security.salt') . $data['User']['password_2']);
        }
        return $this->save($data);
    }

}
?>